为了管理分页,包括条件查询分页。用一个自定义标签方式,是一个很好的方案。

NewPage.java  定义一个分页类

package com.oa.util.newpage;import java.util.List;/** * * @author geQ * 分页类 * @param 
*/ public class NewPage
{ //需要获取参数有: 每页条数,当前页,总条数 private int pageSize;//每页条数 private int firstPage = 1;//首页 private int previousPage;//上一页 private int currentPage;//当前页 private int nextPage;//下一页 private int lastPage;//尾页 private int pageCount;//总页数 private int totalCount;//总条数 private List
list;//只用来存放当前页要现实的数据内容记录 //一键设置其他参数... public void setOthers(){ //计算总页数 pageCount this.pageCount = (this.totalCount-1)/this.pageSize+1; //下一页 if((this.currentPage+1)<=this.pageCount){ this.nextPage = this.currentPage+1; }else{ this.nextPage = this.currentPage; } //上一页 if(this.currentPage>1){ this.previousPage = this.currentPage-1; }else{ this.previousPage = this.currentPage; } //设置尾页 this.lastPage = this.pageCount; } //快捷设置初始化参数,也可以用其他单个的设置,参数依次为:当前页,每页条数,总条数 public void setFast(int currentPage,int pageSize,int total){ this.currentPage = currentPage; this.pageSize = pageSize; this.totalCount = total; setOthers(); } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getFirstPage() { return firstPage; } public void setFirstPage(int firstPage) { this.firstPage = firstPage; } public int getPreviousPage() { return previousPage; } public void setPreviousPage(int previousPage) { this.previousPage = previousPage; } public int getCurrentPage() { return currentPage; } public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } public int getNextPage() { return nextPage; } public void setNextPage(int nextPage) { this.nextPage = nextPage; } public int getLastPage() { return lastPage; } public void setLastPage(int lastPage) { this.lastPage = lastPage; } public int getPageCount() { return pageCount; } public void setPageCount(int pageCount) { this.pageCount = pageCount; } public int getTotalCount() { return totalCount; } public void setTotalCount(int totalCount) { this.totalCount = totalCount; } public List
getList() { return list; } public void setList(List
list) { this.list = list; } }

定义一个标签核心类PagerTag.java

package com.oa.util.newpage;import java.io.IOException;import javax.servlet.http.HttpServletRequest;import javax.servlet.jsp.JspException;import javax.servlet.jsp.JspWriter;import javax.servlet.jsp.tagext.TagSupport;public class PagerTag extends TagSupport{    /**     *     */    private static final long serialVersionUID = 1L;    private String url;//请求的action    private int pageSize;//每页的条数    private int currentPage;//当前页    private int pageCount;//总页数    private int totalCount;//总条数    private String param;//接受参数的键数组                                                                public String getParam() {        return param;    }    public void setParam(String param) {        this.param = param;    }    public String getUrl() {        return url;    }    public void setUrl(String url) {        this.url = url;    }    public int getPageSize() {        return pageSize;    }    public void setPageSize(int pageSize) {        this.pageSize = pageSize;    }    public int getCurrentPage() {        return currentPage;    }    public void setCurrentPage(int currentPage) {        this.currentPage = currentPage;    }    public int getPageCount() {        return pageCount;    }    public void setPageCount(int pageCount) {        this.pageCount = pageCount;    }    public int getTotalCount() {        return totalCount;    }    public void setTotalCount(int totalCount) {        this.totalCount = totalCount;    }    public int doEndTag() throws JspException{        return this.EVAL_PAGE;    }    public int doStartTag() throws JspException{        HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();        String path = request.getSession().getServletContext().getRealPath("\\");        JspWriter jw = pageContext.getOut();        StringBuilder sb = new StringBuilder();//拼写输出的HTML文本        sb.append("
\r\n") .append("
\r\n") .append("
\r\n")//将得到的每页条数记入form .append("
\r\n")//将得到的当前页记入form .append("
\r\n")//将得到的总页数记入form .append("
\r\n");//将得到的总条数记入form if(param!=null){ String [] keyValue = param.split(";"); for (int i = 0,len=keyValue.length; i < len; i++) { if(!keyValue[i].equals("")){ if(keyValue[i].split("=").length==1){ sb.append("
\r\n");//将参数循环记入form }else{ sb.append("
\r\n");//将参数循环记入form } } } } sb.append("
\r\n"); /* * 分页代码的html文本 * */ System.out.println(); //显示首页 sb.append("首页\r\n"); //如果当前页大于1,输出前页文本 if(this.currentPage>1) sb.append("前页\r\n"); //从当前页前两页开始显示 if(this.currentPage-2>0) sb.append(""+(this.currentPage-2)+"\r\n"); //当前页前一页 if(this.currentPage>1) sb.append(""+(this.currentPage-1)+"\r\n"); //当前页 sb.append(""+(this.currentPage)+"\r\n"); //当前页的 后页大于总页数时显示 if(this.currentPage+1<=pageCount) sb.append(""+(this.currentPage+1)+"\r\n"); //当前页的 后两页大于总页数时显示 if(this.currentPage+2<=pageCount) sb.append(""+(this.currentPage+2)+"\r\n"); //当前页+1大于总页数时显示 if(this.currentPage+1<=pageCount) sb.append("后页\r\n"); //显示尾页 sb.append("尾页\r\n"); sb.append("每页") .append("条 \r\n") .append("共"+pageCount+"页 共"+totalCount+"条\r\n") .append("跳转到\r\n") .append("\r\n") .append("\r\n") .append(""); /* * 对分页的css样式控制 */ sb.append("\r\n"); /* * 对分页的js控制 * $submitMyPage方法进行页面跳转 * selectPagesize方法进行每页条数的选择 * jump方法进行页面的静态调整 * go方法跳转所输入页面 */ sb.append("\r\n"); sb.append(""); try { jw.print(sb.toString()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return this.SKIP_BODY; }}

写一个pageTag.tld文件,将文件放到web-inf下

1.0
p
http://pagetld
page
com.oa.util.newpage.PagerTag
empty
url
true
true
java.lang.String
pageSize
true
true
java.lang.Integer
currentPage
true
true
java.lang.Integer
pageCount
true
true
java.lang.Integer
totalCount
true
true
java.lang.Integer
param
false
true
java.lang.String

定义一个分页dao

package com.oa.dao.newPage.daoImp;import java.util.List;import org.hibernate.HibernateException;import org.hibernate.Query;import org.hibernate.Session;import org.hibernate.Transaction;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import com.oa.dao.newPage.daoInter.NewPageDaoInter;import com.oa.util.newpage.NewPage;/** * A data access object (DAO) providing persistence and search support for * PersonnelContract entities. Transaction control of the save(), update() and * delete() operations can directly support Spring container-managed * transactions or they can be augmented to handle user-managed Spring * transactions. Each of these methods provides additional information for how * to configure it for the desired type of transaction control. * * @see com.oa.model.zz.PersonnelContract * @author MyEclipse Persistence Tools * @param 
*/@SuppressWarnings("unchecked")public class NewPageDao
extends HibernateDaoSupport implements NewPageDaoInter{ private static final Logger log = LoggerFactory .getLogger(NewPageDao.class); public List
findBySql(String sql) { Session session = this.getHibernateTemplate().getSessionFactory().openSession(); List
list = session.createQuery(sql).list(); return list; }/** 查询以及分页*/ public long totalCount(String sql){ List
list = getHibernateTemplate().find(sql); return list.get(0); } public NewPage
findAll(String sql1,String sql2,String currentPage,String pageSize){ int totalNum = (int)totalCount(sql1); Session session = this.getHibernateTemplate().getSessionFactory().openSession(); NewPage
page = new NewPage
(); if(currentPage==null||currentPage.equals("")){ currentPage = "1"; } if(pageSize==null||pageSize.equals("")){ pageSize = "10"; } Transaction tx = session.beginTransaction(); page.setFast(Integer.parseInt(currentPage), Integer.parseInt(pageSize), totalNum); Query query = session.createQuery(sql2); query.setFirstResult((Integer.parseInt(currentPage)-1)*Integer.parseInt(pageSize)); query.setMaxResults(Integer.parseInt(pageSize)); page.setList((List
)query.list()); tx.commit(); session.close(); return page; }}

导入jq库文件

使用说明:

1、将 pageTag.tld 文件放到web-inf下。

2、newPage为封装好的baseDao.其中包含了dao的接口与实现。将此文件根据个人情况放入src下即可。

3、将page文件夹放到webroot下即可。里边主要是分页的图片以及jquery库文件

4、NewPage.java是对分页的具体的封装。根据个人情况放入src下的包即可。

5、PagerTag.java是分页的核心文件,将此文件放入工程下的src/com/oa/util/newpage即可。

刚导入工程可能有个别文件报错,将文件中对类的引用的包进行修改即可。

最后在jsp页面上面导入标签:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>这个是c标签

<%@ taglib prefix="p" uri="http://pagetld" %>这个为自定义的分页标签

在你需要分页的地方写上标签:

<p:page totalCount="${page.totalCount}" pageSize="${page.pageSize}" pageCount="${page.pageCount}" url="${url}" currentPage="${page.currentPage}"/>

此标签需要 在action传参数:总条数:totalCount,每页条数:pageSize,需要跳转的action;url.当前页:currentPage,总页数:pageCount,

如果需要条件查询:在标签内加入param属性:例如

<p:page totalCount="${page.totalCount}" pageSize="${page.pageSize}" pageCount="${page.pageCount}" url="${url}" currentPage="${page.currentPage}" param="name=zhangsan;age=14"/>

以key=value;key=value进行传值。